package com.lvyq.myplayer.common.util

import android.content.Context
import com.lvyq.myplayer.common.shared.MainAudioSharedPrefsManager
import com.lvyq.myplayer.dao.CurrentPlayLstDao
import com.lvyq.myplayer.database.PlayerDatabase
import com.lvyq.myplayer.model.current.CurrentPlayLstEntity
import com.lvyq.myplayer.parcelable.SongInfoData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.UUID

/**
 * 文件描述：Utils
 * 作者：lvyq
 * 创建时间：2025-06-25
 * 修改时间：2025-06-25
 */
open class DataOperationUtils {

    companion object {

        //播放列表
        private fun getCurrentPlayLstDao(context: Context): CurrentPlayLstDao {
            return PlayerDatabase.getDatabase(context).currentPlayLstDao()
        }

         fun installCurrentPlayLst(context: Context,list: ArrayList<SongInfoData>?) {

             CoroutineScope(Dispatchers.IO).launch {
                //清空列表
                getCurrentPlayLstDao(context).deleteAll()
                val currentTime  = System.currentTimeMillis()
                // 批量构建实体并插入
                list?.let { songs ->
                    val entities = songs.mapIndexed { index, song ->
                        CurrentPlayLstEntity(
                            id= UUID.randomUUID().toString(),
                            title = song.title.orEmpty(),
                            businessKey = song.id,
                            singer = song.singer,
                            bgImg = song.bgImg.orEmpty(),
                            url = song.song,
                            lrc = song.lrc,
                            singerId = song.singerId,
                            publishDate = song.publishDate,
                            albumId = song.albumId,
                            albumName = song.albumName,
                            createTime = currentTime,
                            updateTime = currentTime,
                            totalDuration = 0,
                            playCount = 1,
                            type = 1,
                            reNum = song.reNum,
                            position = index
                        )
                    }
                    getCurrentPlayLstDao(context).insertBatch(entities)
                }

            }
        }

        fun delWithPositionAndReSet(context: Context,position: Int?) {
            CoroutineScope(Dispatchers.IO).launch {
                //清空列表
                getCurrentPlayLstDao(context).deleteByPosition(position)
                getCurrentPlayLstDao(context).updPositionAfterDel(position)
                }

            }

        /**
         * 添加到下一首
         */
        fun addWithPositionAndReSet(context: Context,song: SongInfoData) {
         var position=  MainAudioSharedPrefsManager.getCurrentAudioPosition(context)
         if (position==null){
             position="0"
         }
            CoroutineScope(Dispatchers.IO).launch {
                var  index = position.toInt()
                if (index>=0){
                    index=index+1
                }else{
                    index=0
                }
                val currentTime  = System.currentTimeMillis()
                //清空列表
               var entity= CurrentPlayLstEntity(
                    id= UUID.randomUUID().toString(),
                    title = song.title.orEmpty(),
                    businessKey = song.id,
                    singer = song.singer,
                    bgImg = song.bgImg.orEmpty(),
                    url = song.song,
                    lrc = song.lrc,
                    singerId = song.singerId,
                    publishDate = song.publishDate,
                    albumId = song.albumId,
                    albumName = song.albumName,
                    createTime = currentTime,
                    updateTime = currentTime,
                    totalDuration = 0,
                    playCount = 1,
                    type = 1,
                    reNum = song.reNum,
                    position = index
                )

                //添加到第一首的后面
                getCurrentPlayLstDao(context).updPositionAfterAdd(position.toInt())
                getCurrentPlayLstDao(context).insert(entity)


            }

        }

    }

}